package com.deprice4u_client.products_on_sale;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.deprice4u_client.R;
import com.deprice4u_client.image_loader.ImageLoader;

public class SocialSearchAdapter extends BaseAdapter implements OnClickListener {
	private Context mContext;
	private LayoutInflater mInflater;
	private int mLayout;
	private ArrayList<SocialDataClass> mListItems;
	private ArrayList<SocialDataClass> mListItemsWithDivision;
	private ArrayList<String> mRelatedKeywords;
	private boolean isExtendedResult = false;
	private HashMap<Integer, String> mapForDivisionAndIndex;

	public ImageLoader imageLoader;

	public SocialSearchAdapter(Context context,
			ArrayList<SocialDataClass> aListItems,
			ArrayList<String> aRelatedKeywords) {
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mListItems = aListItems;
		imageLoader = new ImageLoader(mContext.getApplicationContext());
		mRelatedKeywords = aRelatedKeywords;
		this.mListItemsWithDivision = this.mListItems;
		
		if (mRelatedKeywords != null) {
			this.mListItemsWithDivision = new ArrayList<SocialDataClass>();
			mapForDivisionAndIndex = new HashMap<Integer, String>();
			this.isExtendedResult = true;
			// Log.i("SM", this.mRelatedKeywords.toString());
			int j = 0;
			String beforeKeyword = mRelatedKeywords.get(j);
			for (int i = 0; i < this.mListItems.size(); i++) {
				String title = this.mListItems.get(i).title;
				if (title.contains(beforeKeyword) != true) {
					this.mListItemsWithDivision.add(null);
					beforeKeyword = mRelatedKeywords.get(++j);
					this.mapForDivisionAndIndex.put(
							this.mListItemsWithDivision.size() - 1,
							beforeKeyword);
				}
				this.mListItemsWithDivision.add(this.mListItems.get(i));
			}
		}
		
		this.mListItems = this.mListItemsWithDivision;
	}

	public int getCount() {
		return (mListItems.size());
	}

	public Object getItem(int nPos) {
		// TODO Need to do fixed!
		return mListItems.get(nPos);
	}

	public long getItemId(int nPos) {
		// TODO Need to do fixed!
		return nPos;
	}

	public View getView(int nPos, View convertView, ViewGroup parent) {
		// Log.i("SM","nPos : "+nPos);
		View vi = convertView;
		SocialDataClass item = mListItems.get(nPos);

		if (item != null) {
			
			/*if (convertView == null)
				*/vi = mInflater.inflate(R.layout.social_list_entry, null);

			TextView priceNow = (TextView) vi.findViewById(R.id.price_now);
			TextView priceOriginal = (TextView) vi.findViewById(R.id.price_original);
			TextView title = (TextView) vi.findViewById(R.id.title);
			TextView area = (TextView) vi.findViewById(R.id.area);
			TextView provider = (TextView) vi.findViewById(R.id.provider);
			ImageView imageView = (ImageView) vi.findViewById(R.id.photo);

			//Log.i("SM", "Price now" + item.price_now);

			priceNow.setText(String.valueOf(item.price_now));
			priceOriginal.setText(String.valueOf(item.price_original));
			title.setText(item.title);
			area.setText(item.area);
			provider.setText(item.social_provider);

			imageLoader.DisplayImage(item.photo_url, imageView);
			vi.setOnClickListener(this);
		} else {
			vi = mInflater.inflate(R.layout.social_list_division, null);
			TextView titleDivision = (TextView) vi
					.findViewById(R.id.title_division);
			titleDivision.setText("Related keyword : "+mapForDivisionAndIndex.get(nPos));

		}

		vi.setTag(nPos);
		return vi;
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		// 111번 라인에서 저장한 tag(position)을 꺼내온다.
		int position = (Integer) v.getTag();

		// 리스트에서 position에 맞는 데이터를 받아온다.
		SocialDataClass data = (SocialDataClass) getItem(position);

		// 다음 액티비티로 넘길 Bundle 데이터를 만든다.
		Bundle extras = new Bundle();
		extras.putString("title", data.title);
		extras.putString("link", data.link);
		extras.putString("time_start", data.time_start);
		extras.putString("time_end", data.time_end);
		extras.putInt("price_original", data.price_original);
		extras.putInt("price_now", data.price_now);
		extras.putString("social_provider", data.social_provider);
		extras.putString("area", data.area);
		extras.putString("photo_url", data.photo_url);

		// 인텐트를 생성한다.
		// 컨텍스트로 현재 액티비티를, 생성할 액티비티로 ItemClickExampleNextActivity 를 지정한다.
		Intent intent = new Intent(mContext, SocialDetailActivity.class);

		// 위에서 만든 Bundle을 인텐트에 넣는다.
		intent.putExtras(extras);

		// 액티비티를 생성한다.
		mContext.startActivity(intent);

	}

}
